package com.sgb.kjwl.view.widget;

import android.content.Context;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.IpPickSelectAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.recyclerview.widget.GridLayoutManager;

public class IpPopWindow extends BasePopWindow {
    private IpPickSelectAdapter adapter;
    private MaxHeightRecyclerView recyclerView;
    private @DrawableRes
    int color;

    public IpPopWindow(Context context) {
        super(context);
    }

    public IpPopWindow(Context context, @DrawableRes int color) {
        super(context);
        this.color = color;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_for_manage;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.goods_type_list);

        if (color == 0) {
            adapter = new IpPickSelectAdapter(mContext, null);
        } else {
            adapter = new IpPickSelectAdapter(mContext, null, color);
        }
        adapter.setItemSelect(true);
        adapter.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<IpPickSelectEntity>() {
            @Override
            public void onItemClick(IpPickSelectEntity ipPickSelectEntity, int position) {
                if (onResultClick != null)
                    onResultClick.result(ipPickSelectEntity, position, null);
                dissMiss();
            }
        });

        holder.getPop().findViewById(R.id.bg_pop_gray_layout).setOnClickListener(view -> dissMiss());
    }

    public void setData(List<IpPickSelectEntity> list) {
        adapter.setDatas(list);
    }

    public void setBackground(@ColorRes int color) {
        recyclerView.setBackgroundResource(color);
    }

    public void setDefItem(int position) {
        adapter.setDefItem(position);
    }
}
